package form.ui
{
	import flash.events.Event;
	
	import form.dispatchers.FormElementDispatcher;
	import form.events.FormElementChangeEvent;
	import form.events.FormElementEvent;
	import form.interfaces.IFormElement;
	import form.ui.core.Button;
	import form.ui.core.ButtonSkin;

	public class FormButton extends Button implements IFormElement
	{		
		private var _formName:String;
		private var _formValue:String;
		
		private var up:ButtonSkin;
		private var over:ButtonSkin;
		private var down:ButtonSkin;
		private var disabl:ButtonSkin;
		
		public function FormButton()
		{
			super();
			
			up = new ButtonSkin();
			up.name = ButtonSkin.UP;
			
			over = new ButtonSkin();
			over.name = ButtonSkin.OVER;
			
			down = new ButtonSkin();
			down.name = ButtonSkin.DOWN;
			
			disabl = new ButtonSkin();
			disabl.name = ButtonSkin.DISABLE;
			
			this.mouseUp = up;
			this.mouseOver = over;
			this.mouseDown = down;
			this.disabled = disabl;
			
			var e:FormElementEvent = new FormElementEvent(FormElementEvent.INITIALIZED);
			e.element = this;
			
			FormElementDispatcher.instance.dispatchEvent(e);
		}
		
		override protected function onAddedToStage(e:Event):void 
		{
			super.onAddedToStage(e);
		}
		
		public function set formName(value:String):void {
			this._formName = value;
		}
		
		public function get formName():String {
			return this._formName;
		}
		
		public function set formValue(value:String):void {
			this._formValue = value;	
		}
		
		public function get formValue():String {
			return this._formValue;
		}
		
		override public function get width():Number
		{
			return mouseUp ? mouseUp.width : super.width;
		}		
		override public function set width(value:Number):void
		{
			if(mouseUp && contains(mouseUp))mouseUp.width = value;
			if(mouseOver && contains(mouseOver))mouseOver.width = value;
			if(mouseDown && contains(mouseDown))mouseDown.width = value;
			if(disabled && contains(disabled))disabled.width = value;
			
			super.width = value;
		}
		
		override public function get height():Number
		{
			return mouseUp ? mouseUp.height : super.height;
		}
		override public function set height(value:Number):void
		{
			if(mouseUp && contains(mouseUp))mouseUp.height = value;
			if(mouseOver && contains(mouseOver))mouseOver.height = value;
			if(mouseDown && contains(mouseDown))mouseDown.height = value;
			if(disabled && contains(disabled))disabled.height = value;
			
			super.height = value;
		}
		
		override public function mouseUpAction():void
		{
			super.mouseUpAction();
			var evt:FormElementChangeEvent = new FormElementChangeEvent(FormElementChangeEvent.FORM_BUTTON_ON);
			evt.element = this;
			dispatchEvent(evt);
		}
	}
}